Imports System.Data.Common

Partial Class Presentation_Control_AssignClient
    Inherits System.Web.UI.Page

    Protected Sub txtSearchName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearchName.TextChanged
        PopulateClientDrop()
        lblSuccess.Visible = False
    End Sub

    Protected Sub PopulateClientFirst()

        Dim FirstClient As New ListItem

        dlClientNames.Items.Clear()
        FirstClient.Text = "Select a Client"
        FirstClient.Value = "0"
        FirstClient.Selected = True
        dlClientNames.Items.Add(FirstClient)

    End Sub

    Protected Sub PopulateClientDrop()

        Dim Sql As String
        Dim Records As DbDataReader
        'Dim Params As New Hashtable
        Dim First As New ListItem
        Dim Params(0) As GenParameter

        If txtSearchName.Text = "" Then
            PopulateClientFirst()
            Return
        End If

        Sql = "Select ClientID, ClientName FROM ClientMaster" & _
        " WHERE ClientName LIKE CONCAT('%',?ClientName,'%')" & _
        " ORDER BY ClientName"

        'Params.Add("?ClientName", txtSearchName.Text.ToString)
        Params(0) = New GenParameter("?ClientName", txtSearchName.Text, Data.DbType.String)

        Records = GenHelper.ExecuteReader(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

        dlClientNames.Items.Clear()
        First.Text = "Select a Client"
        First.Value = "0"
        First.Selected = True
        dlClientNames.DataSource = Records
        dlClientNames.DataBind()
        dlClientNames.Items.Add(First)
        Records.Close()

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim Sql As String
        Dim Records As DbDataReader


        If Not Page.IsPostBack Then

            'Populate the Executives List
            dlExecutives.DataTextField = "ExecutiveName"
            dlExecutives.DataValueField = "ExecutiveID"

            PopulateClientFirst()

            Sql = "SELECT ExecutiveID, ExecutiveName FROM ExecutiveMaster WHERE" & _
                    " Active = 1 ORDER BY ExecutiveName"


            Records = GenHelper.ExecuteReader(Sql, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())
            dlExecutives.DataSource = Records
            dlExecutives.DataBind()
            Records.Close()

        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ExecutiveToAssignClient As New Executive(dlExecutives.SelectedValue)

        Try

            ExecutiveToAssignClient.AssignClient(dlClientNames.SelectedValue)

        Catch ex As Exception

            lblSuccess.Text = ex.Message
            lblSuccess.Visible = True
            lblSuccess.ForeColor = Drawing.Color.Red
            Exit Sub

        End Try

        lblSuccess.Visible = True
        lblSuccess.ForeColor = Drawing.Color.Black

    End Sub
End Class
